package view.items;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;

import constants.Icons;
import controller.CompleteListener;

import view.BaseComponent;

public class OptionsPanel extends BaseComponent implements ActionListener {

	private JButton filter;
	private JButton waitTill;
	private JButton nextNode;
	
	private boolean filterActive;
	
	public OptionsPanel() {
		
		filter = new JButton(Icons.getIcon("filterInactive"));
		filter.setActionCommand("filter");
		waitTill = new JButton(Icons.getIcon("waitTill"));
		waitTill.setActionCommand("waitTill");
		nextNode = new JButton(Icons.getIcon("nextNode"));
		nextNode.setActionCommand("nextNode");
		
		filterActive = false;
		
		filter.addActionListener(this);
		
		filter.setMaximumSize(new Dimension(filter.getIcon().getIconWidth()+4,filter.getIcon().getIconHeight()+4));
		filter.setPreferredSize(new Dimension(filter.getIcon().getIconWidth()+4,filter.getIcon().getIconHeight()+4));
		waitTill.setMaximumSize(new Dimension(waitTill.getIcon().getIconWidth()+4,waitTill.getIcon().getIconHeight()+4));
		waitTill.setPreferredSize(new Dimension(waitTill.getIcon().getIconWidth()+4,waitTill.getIcon().getIconHeight()+4));
		nextNode.setMaximumSize(new Dimension(nextNode.getIcon().getIconWidth()+4,nextNode.getIcon().getIconHeight()+4));
		nextNode.setPreferredSize(new Dimension(nextNode.getIcon().getIconWidth()+4,nextNode.getIcon().getIconHeight()+4));
		
		this.setBorder(BorderFactory.createTitledBorder("Commands"));
		this.setLayout(new BoxLayout(this,BoxLayout.LINE_AXIS));
		
		this.add(Box.createHorizontalStrut(3));
		this.add(filter);
		this.add(Box.createHorizontalStrut(5));
		this.add(waitTill);
		this.add(Box.createHorizontalStrut(5));
		this.add(nextNode);
		this.add(Box.createHorizontalGlue());
		
		this.setMaximumSize(new Dimension(90,80));
		this.setMinimumSize(new Dimension(90,80));
		this.setPreferredSize(new Dimension(90,80));
	}

	public boolean hasFilterActive() {
		return filterActive;
	}
	
	@Override
	public void attachListener(CompleteListener l) {
		filter.addActionListener(l);
		waitTill.addActionListener(l);
		nextNode.addActionListener(l);
	}

	@Override
	public void setEnabled(boolean enable) {
		filter.setEnabled(enable);
		waitTill.setEnabled(enable);
		nextNode.setEnabled(enable);
	}

	public void actionPerformed(ActionEvent arg0) {
		if (filterActive)
			filter.setIcon(Icons.getIcon("filterInactive"));
		else
			filter.setIcon(Icons.getIcon("filterActive"));
		filterActive = !filterActive;
	}
}
